//
//  TransformViewController.swift
//  testRxSwift
//
//  Created by 火星人 on 2017/7/28.
//  Copyright © 2017年 火星人. All rights reserved.
//

import UIKit
import RxGesture
import RxCocoa
import RxSwift

class TransformViewController: BaseViewController {

   
    let disposed = DisposeBag()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let tapView = UIView(frame: CGRect(x: 0, y: 80, width: 80, height: 80))
        tapView.backgroundColor = UIColor.randomColor()
        self.view.addSubview(tapView)
        
        tapView.centerInSuperView()
        
        let transform = tapView.rx.transformGestures().shareReplay(1)
        
        var trans = tapView.transform
        
        transform.when(.began)
            .asTransform()
            .subscribe(onNext: { (transform, _) in
                trans = tapView.transform
            })
            .disposed(by: disposed)
        
        transform.when(.changed)
            .asTransform()
            .subscribe(onNext: { (transform, _) in
                tapView.transform = CGAffineTransform(translationX: trans.tx + transform.tx, y: trans.ty + transform.ty)
            })
            .disposed(by: disposed)
        
        transform.when(.ended)
            .asTransform()
            .subscribe(onNext: { (transform, _) in
                print("transform ended", tapView.frame)
            })
            .disposed(by: disposed)
        
    
    }

}
